var upload={
	error:function(msg){
		common_ops.alert(msg);
	},
	success:function(image_key){
		$html='<img src="'+common_ops.buildPicUrl('brand',image_key)+'">'+
		'<span class="fa fa-times-circle del del_image" data='+image_key+'><i></i></span>';
		if($(".upload_pic_wrap .pic-each").size()>0){
			$(".upload_pic_wrap .pic-each").html($html);
		}else{
			$(".upload_pic_wrap").append('<span class="pic-each">'+$html+'</span>');
		}
		brand_set_ops.delete_img();
	}	
};
var brand_set_ops={
	init:function(){
		this.eventBind();
		this.delete_img();
	},
	eventBind:function(){
		$(".save").click(function(){
			var btn_target=$(this);
			if(btn_target.hasClass("disabled")){
				common_ops.alert("正在处理");
				return false;
			}
			var name_target=$("input[name=name]");
			var name=name_target.val();

			var logo_target=$("input[name=logo]").val();

			var mobile_target=$("input[name=mobile]");
			var mobile=mobile_target.val();
			var image_key=$(".pic-each .del_image").attr("data");
			
			var address_target=$("input[name=address]");
			var address=address_target.val();

			var description_target=$("textarea[name=description]");
			var description=description_target.val();
			
			if(name.length < 1){
				common_ops.tip('名称非法',name_target);
				return false;
			}
			
			if(mobile.length < 1){
				common_ops.tip('电话非法',mobile_target);
				return;
			}
			if(address.length < 1){
				common_ops.tip('地址非法',address_target);
				return;
			}
			if($(".upload_pic_wrap .pic-each").size()<1){
				common_ops.alert("没有上传品牌图片");
				return;
			}

			if(description.length < 1){
				common_ops.tip('描述非法',description_target);
				return;
			}
			btn_target.addClass("disabled");
			$.ajax({
				url:common_ops.buildWebUrl('/brand/set'),
				type:"POST",
				data:{
					name:name,
					mobile:mobile,
					image_key:image_key,
					address:address,
					description:description
				},
				dataType:"json",
				success:function(res){
					btn_target.removeClass("disabled");
					var callback=null;
					if(res.code==200){
						callback=function(){
						window.location.href=window.location.href;
						};
					}
					common_ops.alert(res.msg,callback);
				},
			});
		});
		$(".upload_pic_wrap input[name=pic]").change(function(){
			$(".upload_pic_wrap").submit();
		});
	},
	delete_img:function(){
		$(".upload_pic_wrap .del_image").unbind().click(function(){
			$(this).parent().remove();
		});
	}
};
$(document).ready(function(){
	brand_set_ops.init();
});